<?php 

$weekStartDay = 'Monday';
$numOfWorkDaysPerWeek = 7;

$startDate = getWeekStartDate($viewDate, $weekStartDay);
$endDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate) + 7, date("Y", $startDate));

$weekDates = array();
for ($i=0; $i<7; $i++)
{
	$newDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate) + $i, date("Y", $startDate));
	$weekDates[] = array('date' => $newDate);
}


function getWeekStartDate($viewDate, $weekStartDay)
{
	$notFound = true;
	$precedingDate = $viewDate;
	while ($notFound)
	{
		$strDay = date("l", $precedingDate);
		if ($strDay == $weekStartDay)
		{
			$notFound = false;
			break;
		}
		$precedingDate = mktime(0, 0, 0, date("m", $precedingDate), date("d", $precedingDate)-1, date("Y", $precedingDate));
	}
	return $precedingDate;
}

?>

<table>
	<!-- display the headers for each day of the work week -->
	<tr>
		<th rowspan="2">Employees</th>
		<!-- short day name (ex: Mon) -->
		<th><?php echo(date("l", $weekDates[0]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[1]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[2]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[3]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[4]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[5]['date'])); ?></th>
		<th><?php echo(date("l", $weekDates[6]['date'])); ?></th>
	</tr>
	<tr>
		<!-- short month + date (ex: Mar 3) -->
		<th><?php echo(date("M\nd", $weekDates[0]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[1]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[2]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[3]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[4]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[5]['date'])); ?></th>
		<th><?php echo(date("M\nd", $weekDates[6]['date'])); ?></th>
	</tr>

	<?php

	$getAccountsResult = getAccounts($userName, $userPassHash);
	
	//grab the accounts
	$accounts = $getAccountsResult['results'];

	//parse the accounts
	foreach ($accounts as &$accountsEntry) {
		$accName = $accountsEntry['accName'];
		$getAccountResult = getAccount($accName, $userName, $userPassHash);
		$_SESSION[$accName . 'account'] = $getAccountResult['results']['dataHash'];
		$checkRangeSchedulePerWorkerResult = checkRangeSchedulePerWorker($userName, $userPassHash, date("Y-m-d", $startDate), date("Y-m-d", $endDate), $accName);
		$shifts = null;
		
		//grab the shifts
		$shifts = $checkRangeSchedulePerWorkerResult[1];
		
		?>
		
		<tr>
			<td><?php echo($accName); ?></td>
		
		<?php
		
		//parse the shifts
		for ($i = 0; $i < $numOfWorkDaysPerWeek; $i++) {
			$hasShift = false;
			foreach ($shifts as &$shiftsEntry) {
				$shiftDate = $shiftsEntry['datestamp'];
				$shiftDate = strtotime($shiftDate);
				$dayInterval = date("d", $shiftDate) - date("d", $startDate);
				if($dayInterval == $i)
				{
					?>
					<td>
						<form action="template.php">
							<table>
								<tr>
									<td>
										<!--hidden fields -->
										<input type="hidden" name="roleID" value="<?php echo($shiftsEntry['roleID']); ?>">
										<input type="hidden" name="dateID" value="<?php echo($shiftsEntry['dateID']); ?>">
										<input type="hidden" name="empID" value="<?php echo($shiftsEntry['empID']); ?>">
									</td>
								<tr>
									<td><?php echo($shiftsEntry['roleName']); ?></td>
									<td><?php echo($shiftsEntry['startTime']); ?></td>
								</tr>
								<tr>
									<td><?php echo($shiftsEntry['shiftStatus']); ?></td>
									<td><?php echo($shiftsEntry['endTime']); ?></td>
								</tr>
								<tr>
									<td></td>
									<td>
										<input type="submit" name="shiftAction" value="Delete Shift" />
									</td>
								</tr>
							</table>
						</form>
					</td>
					
					<?php
					
					$hasShift = true;
				}
			}
			if(!$hasShift)
			{
				?>
				<td></td>
				<?php
			}
		}
		?>
		</tr>
		<?php
	}
	
	?>
	
</table>
